<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/7/21
 * Time: 16:59
 */
namespace app\api\controller;
use app\models\Models;

class Goods extends Base {
    /**
     * 商品列表
     * @return \think\response\Json
     */
    public function goodsList(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt','type_id','business_id']);
                $goods = new Models('goods');
                $where = ['bid'=>$params['business_id'],'type_id'=>$params['type_id'],'is_delete'=>Models::DELETE_NOT,'status'=>[['eq',Models::GOODS_STATUS_ON],['eq',Models::GOODS_SALES_ALL],'or']];
                $list = $goods->_select($where,'id,goods_no,name,price,descrip,image,sales_amount,sales_month');
                if($params['type_id'] == Models::GOODS_TYPE_COMBO){
                    // 获取套餐信息
                    $ids = implode(',',array_column($list,'id'));
                    $goods_combo = new Models('goods_combo');
                    $goods_arr = $goods_combo->joinMany([['goods b','a.gid = b.id','left']],['a.cid'=>['in',$ids]],'a.gid,a.cid,a.qty,b.name');
                }
                foreach($list as $k=>$v){
                    $list[$k]['image'] = empty($v['image']) ? '' : self::arrayMapGoods($v['image']);
                    if($params['type_id'] == Models::GOODS_TYPE_COMBO){
                        foreach($goods_arr as $k1=>$v1){
                            if($v['id'] == $v1['cid']){
                                unset($v1['gid'],$v1['cid']);
                                $list[$k]['combo'][] = $v1;
                            }
                        }
                    }
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }
    /**
     * 商品类型列表
     * @return \think\response\Json
     */
    public function goodsTypeList(){
        $params = input('param.');
        try{
            // 参数验证
            self::sign(['business_id']);
            $goods_type = new Models('goods_type');
            $list = $goods_type->_select(['bid'=>[['eq',0],['eq',$params['business_id']],'or'],'status'=>Models::STATUS_ON],'id,name');
        } catch (\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($list));
    }
}